package org.carl.component.job.service

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.xxl.job.core.biz.model.ReturnT
import org.carl.component.job.constant.PlatForm
import org.carl.component.job.util.MapUtil
import org.carl.component.job.vo.XxlConfigVo
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.client.RestTemplate
import java.util.*


/**
 * email carl.chen@bk.ru
 * @Author: 陈青松
 * @Date: 2019/1/19 2:57
 */
open abstract class JobService {
    @Autowired
    protected val restTemplate: RestTemplate? = null
    @Autowired
    protected val xxlConfigVo: XxlConfigVo? = null

    companion object {
        /**
         * 处理头信息
         */
        fun handleHttpHeaders(): HttpHeaders {
            val headers = HttpHeaders()
            headers.put(HttpHeaders.COOKIE, listOf(PlatForm.COOKIE))
            return headers
        }

        /**
         * 处理get数据
         */
        fun handleGetHttpEntity(body: Any): HttpEntity<Map<*, *>?> {
            val headers = handleHttpHeaders()
            return handleRequest(headers, body)
        }

        /**
         * 处理数据
         */
        fun handleHttpEntity(body: Any): HttpEntity<Map<*, *>?> {
            val headers = handleHttpHeaders()
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED)
            return handleRequest(headers, body)
        }

        /**
         * 处理request请求
         */
        fun handleRequest(headers: HttpHeaders, body: Any): HttpEntity<Map<*, *>?> {
            var xxlJobInfoMap: Map<*, *>? = null
            body?.let {
                xxlJobInfoMap = MapUtil.obj2Map(it)
            }
            val request = HttpEntity(xxlJobInfoMap, headers)
            return request
        }

        /**
         * 获取ReturnT
         */
        fun handleResponseEntity(re: ResponseEntity<String>): ReturnT<*>? {
            var rt: ReturnT<*>? = null
            re?.let {
                rt = JSON.parseObject(it.body.toString(), ReturnT::class.java)
            }
            return rt
        }

        /**
         * 获取list或者object
         */
        fun <T : Any?> getObject(rt: ReturnT<*>, clazz: Class<T>): Any? {
            var ts = mutableListOf<T>()
            var t: T? = null
            if (rt.code == 200) {
                if (rt.content is JSONArray) {
                    val jas: JSONArray = rt.content as JSONArray
                    jas?.forEach { i ->
                        t = JSONObject.parseObject(JSON.toJSONString(i), clazz)
                        ts.add(t!!)
                    }
                    return ts
                } else {
                    return JSONObject.parseObject(JSON.toJSONString(rt.content), clazz)!!
                }
            }
            return null
        }
    }

    /**
     * 获取地址
     */
    fun getLoadUrl(method: String): String {
        var addresses: String? = xxlConfigVo!!.addresses
        val jobAdminUrl = addresses!!.split(',')
        val length: Int = jobAdminUrl!!.size
        val random = Random()
        val i = random.nextInt(length)
        return jobAdminUrl!![i] + method
    }
}